//
//  TMToCommentController.m
//  国学商家管理
//
//  Created by Tao Li on 2018/6/29.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "TMToCommentController.h"
#import "TMToCompletedCell.h"
#import "TMWillCommentController.h"
#import "TMUserViewModel.h"
#import "TMMyOrderModel.h"
#import "TMUserCompanitController.h"
#import "TMApplyMoneyBackController.h"
@interface TMToCommentController ()
@property (nonatomic, strong) TMUserViewModel *viewModel;
@property (nonatomic, strong) NSMutableArray<TMMyOrderModel *> *listArray;
@property (nonatomic, strong) WZNoResultView *noResultView;
@end

@implementation TMToCommentController
- (NSMutableArray *)listArray{
    if (!_listArray) {
        _listArray = [NSMutableArray array];
    }
    return _listArray;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self getdata];
}
- (void)dealloc{
    NSLog(@"TMToCommentController");
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.tableFooterView = [UIView new];
    if (self.compelteStateSet == CompelteStateWait) {
        self.title = @"待完成";
    }
    if (self.compelteStateSet == CompelteStateOver) {
        self.title = @"完成订单";
    }
    if (self.compelteStateSet == CompelteStateComment) {
        self.title = @"待评价";
    }
    if (self.compelteStateSet == CompelteStateTakeGoods) {
        self.title = @"待收货";
    }
    if (self.compelteStateSet == CompelteStateRefund) {
        self.title = @"退款";
    }
    __weak typeof(self) weakSelf =  self;
    self.tableView.backgroundColor = [UIColor hx_colorWithHexRGBAString:@"#F8F8F8"];
    [self.tableView registerNib:[UINib nibWithNibName:@"TMToCompletedCell" bundle:nil] forCellReuseIdentifier:@"TMToCompletedCell"];
    self.viewModel = [[TMUserViewModel alloc]init];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf.viewModel getMyOrderWithStatus:weakSelf.compelteStateSet Success:^(id responseObject) {
            weakSelf.listArray = responseObject;
             [weakSelf noResultViewShowORNot];
            [weakSelf.tableView reloadData];
            [weakSelf.tableView.mj_header endRefreshing];
        } failed:^(NSString *error) {
            [weakSelf.tableView.mj_header endRefreshing];
             [weakSelf noResultViewShowORNot];
            [weakSelf.tableView reloadData];
        }];
    }];
//    [self.tableView.mj_header beginRefreshing];
}
- (void)getdata{
 [self.viewModel getMyOrderWithStatus:self.compelteStateSet Success:^(id responseObject) {
    self.listArray = responseObject;
     [self noResultViewShowORNot];
    [self.tableView reloadData];
    //[self.tableView.mj_header endRefreshing];
 } failed:^(NSString *error) {
    //[self.tableView.mj_header endRefreshing];
      [self noResultViewShowORNot];
    [self.tableView reloadData];
 }];
}

#pragma mark - Table view data source

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.compelteStateSet == 1) {
        return 216-50;
    }
    if (self.compelteStateSet == 2 ||self.compelteStateSet == 3 ) {
        return 216;
    }
    if (self.compelteStateSet == 4) {
         TMMyOrderModel *models = self.listArray[indexPath.row];
        if (models.ret == 1 || models.ret == 2) {
            return 216;
        }else{
        //self.model.ret == 2
        //return 216-61.5-13.5;
       return 216-61.5-13.5;
        }
    }
    return 0.1f;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.listArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TMToCompletedCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TMToCompletedCell" forIndexPath:indexPath];
    TMMyOrderModel *models = self.listArray[indexPath.row];
    cell.states = self.compelteStateSet;
    cell.model = models;
     //退款
    __weak typeof(self) weakself = self;
    cell.backBlock = ^{
        if (weakself.compelteStateSet == 1) {
            //退款。
            TMApplyMoneyBackController *vc = [[UIStoryboard storyboardWithName:@"Mine" bundle:nil] instantiateViewControllerWithIdentifier:@"TMApplyMoneyBackController"];
            vc.bussinessName = models.name;
            vc.oid = models.oid;
            [weakself.navigationController pushViewController:vc animated:YES];
        }
        if (weakself.compelteStateSet == 2) {
            //投诉。
            TMUserCompanitController *vc = [[UIStoryboard storyboardWithName:@"Mine" bundle:nil] instantiateViewControllerWithIdentifier:@"TMUserCompanitController"];
            vc.isBussiness = YES;
            vc.models = models;
            [weakself.navigationController pushViewController:vc animated:YES];
        }
    };
     //评价
    cell.comBlock = ^{
        if (weakself.compelteStateSet == 1) {
            //待完成。
            [weakself.viewModel getSureCommpleteWith:models.oid Success:^(id responseObject) {
                TMToCommentController *vc = [[UIStoryboard storyboardWithName:@"Mine" bundle:nil] instantiateViewControllerWithIdentifier:@"TMToCommentController"];
                vc.compelteStateSet = CompelteStateComment;
                [weakself.navigationController pushViewController:vc animated:YES];
            } AndFiled:^(NSString *error) {
                
            }];
        }
        if (weakself.compelteStateSet == 2) {
            //确认评价。
            TMWillCommentController *vc = [[UIStoryboard storyboardWithName:@"Mine" bundle:nil] instantiateViewControllerWithIdentifier:@"TMWillCommentController"];
            vc.models = models;
            [weakself.navigationController pushViewController:vc animated:YES];
        }
        if (weakself.compelteStateSet == 3) {
            //投诉
    TMUserCompanitController *vc = [[UIStoryboard storyboardWithName:@"Mine" bundle:nil] instantiateViewControllerWithIdentifier:@"TMUserCompanitController"];
            vc.isBussiness = YES;
            vc.models = models;
        [weakself.navigationController pushViewController:vc animated:YES];
        }
        if (weakself.compelteStateSet == 4) {
            //取消退款
            [weakself.viewModel getMyCanaleMoneyBack:models.oid Success:^(id responseObject) {
                [weakself performSelector:@selector(getdata) withObject:nil afterDelay:1.5f];
            } failed:^(NSString *error) {
                
            }];
        }
        
    };
    return cell;
}
- (WZNoResultView *)noResultView{
    if (!_noResultView) {
        _noResultView = [[[NSBundle mainBundle] loadNibNamed:@"WZNoResultView" owner:nil options:nil]firstObject];
        if (kWindowH == 812.0) {
            _noResultView.frame = CGRectMake(0, 0, kWindowW, kWindowH);
        }else {
            _noResultView.frame = CGRectMake(0, 0, kWindowW, kWindowH);
        }
        _noResultView.isSearchVC = YES;
        [self.tableView addSubview:_noResultView];
    }
    return _noResultView;
}
- (void)noResultViewShowORNot{
    if (!self.listArray.count) {
        self.noResultView.hidden = NO;
    }else{
        self.noResultView.hidden = YES;
    }
}
@end
